﻿#include "dtutablemodel.h"

DTUTableModel::DTUTableModel()
{

}

DTUTableModel::~DTUTableModel()
{

}


Qt::ItemFlags DTUTableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;
    return QAbstractTableModel::flags(index);
}

QVariant DTUTableModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole) {
         const DTUData& dtu = m_list[index.row()];
         switch(index.column())
         {
            case 0 : return dtu.name;
            case 1 : return dtu.current[0];
            case 2 : return dtu.current[1];
            case 3 : return dtu.current[2];
            case 4 : return dtu.current[3];
            default: break;
         }
     }
     return QVariant();
}

QVariant DTUTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        if(orientation == Qt::Horizontal)
            return m_horizontalHeader.at(section);
        if(orientation == Qt::Vertical)
            return section;
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}

int DTUTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_list.size();
}

int DTUTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_horizontalHeader.size();
}

//resizable
bool DTUTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(role);
    DTUData& dtu = m_list[index.row()];
    switch(index.column())
    {
       case 0 : dtu.name = value.toString(); return true;
       case 1 :
       case 2 :
       case 3 :
       case 4 : dtu.current[index.column()] = value.toInt(); return true;
       default: return false;
    }
    emit dataChanged(index, index);
}

bool DTUTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(parent, row, row + count - 1);
    for(int i=row; i<row+count; i++)
        m_list.insert(i,DTUData());
    endInsertRows();
    return true;
}

bool DTUTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    for(int i=row+count-1;i>=row;i--)
        m_list.removeAt(i);
    endRemoveRows();
    return true;
}

//own
void DTUTableModel::setHorizontalHeader(const QStringList &headers)
{
    m_horizontalHeader = headers;
}

void DTUTableModel::setList(const QList<DTUData> &datalist)
{
    m_list = datalist;
}

void DTUTableModel::updateCurrent(uint id, const QList<int>& data)
{
    int idx=0;
    for(;idx<m_list.size();idx++)
        if(m_list[idx].id == id)
            break;

    for(int i=0;i<4;i++)
        m_list[idx].current[i] = data[i];

    QModelIndex bg = index(idx, 1);
    QModelIndex ed = index(idx, 4);
    emit dataChanged(bg, ed);
}

bool DTUTableModel::addDevice(const DTUData &data)
{
    insertRow(m_list.size());
    m_list.back() = data;
    return true;
}

bool DTUTableModel::deleteDevice(const QModelIndex &index)
{
    removeRow(index.row());
    return true;
}

uint DTUTableModel::getDeviceId(const QModelIndex& index) const
{
    return m_list[index.row()].id;
}

const DTUData& DTUTableModel::getDeviceData(const QModelIndex& index) const
{
    return m_list[index.row()];
}
void DTUTableModel::setDeviceData(const QModelIndex& idx, const DTUData& data)
{
    m_list[idx.row()] = data;
    QModelIndex bg = index(idx.row(),0);
    QModelIndex ed = index(idx.row(),4);
    emit dataChanged(bg, ed);
}

